package cn.stylefeng.guns.modular.auth.domain.visitor;

import cn.stylefeng.guns.modular.auth.domain.composite.PriorityNode;
import cn.stylefeng.guns.modular.auth.domain.dto.AuthPriorityDTO;
import cn.stylefeng.guns.modular.auth.entity.AuthPriorityEntity;
import cn.stylefeng.guns.modular.auth.service.AuthPriorityService;
import cn.stylefeng.guns.modular.auth.utils.converter.AuthPriorityDTO2NodeConverter;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.mapstruct.factory.Mappers;

import java.util.List;

/**
 * @author allen
 * @version 1.0
 * @date 2021/4/15 17:44
 */
@Slf4j
@AllArgsConstructor
public class PriorityNodeRemoveVisitor implements PriorityNodeVisitor {


    private final AuthPriorityService authPriorityService;

    @Override
    public void visit(PriorityNode priorityNode) {


        List<AuthPriorityDTO> children = authPriorityService.getPrioritiesByParentId(priorityNode.getId());

        List<PriorityNode> priorityNodes = AuthPriorityDTO2NodeConverter.INSTANCE.sourceToTarget(children);
        for (PriorityNode node : priorityNodes) {
            node.accept(this);
        }

        boolean b = authPriorityService.removeById(priorityNode.getId());
        if (!b) {
            log.error("删除priority node失败 nodeId：{}",priorityNode.getId());
        }

    }
}
